package edu.hnu.cloudflightserver.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import edu.hnu.cloudflight.android.domain.Flight;
import edu.hnu.cloudflight.android.domain.FlightWarnModel;
import edu.hnu.cloudflightserver.dao.FlightServerRecordDao;
import edu.hnu.cloudflightserver.util.DataBaseUtil;
import edu.hnu.cloudflightserver.util.DateUtil;
import edu.hnu.cloudflightserver.util.StringUtil;

public class FlightServerRecordDaoImpl implements FlightServerRecordDao {

	public boolean recordQuery(String type,Flight f, String userName,String queryDate) {
		if (f == null) {
			return false;
		}
		String sql = "insert into "+FlightWarnModel.table_queryhistory+"(name,flightnumber,start,end,querydate,recorddate,type) values(?,?,?,?,?,?,?)";
		Connection conn = DataBaseUtil.getConnection();
		PreparedStatement ps =null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, userName);
			ps.setString(2, f.name);
			ps.setString(3, f.start);
			ps.setString(4, f.end);
			ps.setString(5,queryDate);
			ps.setString(6, DateUtil.getNowDateToString());
			ps.setString(7, type);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally{
			DataBaseUtil.releaseConnection(conn, ps, null);
		}
		return true;
	}

	public boolean recordWarnTask(Flight f, String userName,String queryDate) {
		if (f == null||StringUtil.isEmpty(userName)) {
			return false;
		}
		String sql = "insert into "+FlightWarnModel.table_warntaskhostory+"(name,flightnumber,start,end,querydate,recorddate) values(?,?,?,?,?,?)";
		Connection conn = DataBaseUtil.getConnection();
		PreparedStatement ps =null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, userName);
			ps.setString(2, f.name);
			ps.setString(3, f.start);
			ps.setString(4, f.end);
			ps.setString(5, queryDate);
			ps.setString(6, DateUtil.getNowDateToString());
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally{
			DataBaseUtil.releaseConnection(conn, ps, null);
		}
		return true;
	}

}
